<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    top="5vh"
    :modal="model"
    :title="title"
    :width="width"
  >
    <vab-query-form>
      <vab-query-form-top-panel :span="24">
        <el-form
          :inline="true"
          label-width="80px"
          :model="tableOptions.queryForm"
        >
          <el-row :gutter="10">
            <el-col :span="6">
              <el-form-item label="岗位编码">
                <el-input
                  v-model="tableOptions.queryForm.jobCode"
                  placeholder="岗位编码"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="岗位名称">
                <el-input
                  v-model="tableOptions.queryForm.jobName"
                  placeholder="岗位名称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="岗位类别:" prop="classId">
                <easy-dict-select
                  ref="classIdRef"
                  :default-value="tableOptions.queryForm.classId"
                  dict-code="1003"
                  show-all="Y"
                  @change="handleClassIdChange"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6" style="text-align: center">
              <el-form-item>
                <el-button
                  icon="refresh-left"
                  type="warning"
                  @click="handleResetQuery"
                >
                  重置
                </el-button>
                <el-button icon="search" type="primary" @click="queryTableData">
                  查询
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      stripe
      :data="tableOptions.dataList"
      :height="tableOptions.height"
      highlight-current-row
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="50"
      />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="岗位编码"
        prop="jobCode"
        sortable
      />
      <el-table-column
        align="center"
        label="岗位名称"
        prop="jobName"
        sortable
      />
      <el-table-column align="center" label="排序号" prop="orderId" sortable />
      <el-table-column align="center" label="岗位类别" prop="classIdDesc" />
      <el-table-column align="center" label="创建人" prop="createUserName" />
      <el-table-column
        align="center"
        :formatter="dateFormat"
        label="创建时间"
        prop="createTime"
      />
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <template #footer>
      <el-button icon="circle-close" type="warning" @click="close">
        取消
      </el-button>
      <el-button icon="circle-check" type="primary" @click="save">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<script setup name="JobPicker.vue">
import EasyDictSelect from "@/components/dictSelect.vue";
import { queryJobTable } from "@/api/admin/org/job/";
import { cloneObject, dateFormat } from "@/utils/common";
import { useTable } from "@/hooks/table";

const props = defineProps({
  title: {
    type: String,
    default: "选择岗位"
  },
  width: {
    type: String,
    default: "60%"
  },
  model: {
    type: Boolean,
    default: true
  },
  multiple: {
    type: Boolean,
    default: function () {
      return true;
    }
  }
});

const emits = defineEmits(["change"]);

const tableRef = ref(null);
const classIdRef = ref(null);

const state = reactive({
  dialogVisible: false
});

const tableOptions = reactive({
  queryForm: {
    jobCode: "",
    jobName: "",
    classId: "ALL"
  },
  multiple: props.multiple,
  queryFunction: queryJobTable,
  descs: ["create_time"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, tableRef);

const handleClassIdChange = val => {
  tableOptions.queryForm.classId = val;
  queryTableData();
};

const show = params => {
  cloneObject(tableOptions.queryForm, {}, params);
  queryTableData();
  state.dialogVisible = true;
};

const close = () => {
  handleReset();
  state.dialogVisible = false;
};

const save = () => {
  emits("change", tableOptions.dataSelections);
  close();
};

defineExpose({
  save,
  show,
  close
});
</script>
<style lang="scss">
.el-container {
  flex-direction: column;
}
</style>
